package com.beep.erp.model.stockIn.stockindetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.StockInDetailBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.recyclerview.adapter.HeaderAndFooterRecyclerViewAdapter;
import com.beep.erp.common.widget.recyclerview.divider.HorizontalDividerItemDecoration;
import com.beep.erp.common.widget.recyclerview.utils.RecyclerViewUtils;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.common.widget.tips.TipsHelper;
import com.beep.erp.model.goodsdetail.GoodsDetailActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 入库单详情页
 */
public class StockInDetailActivity extends ToolbarActivity<StockInDetailPresenter, StockInDetailModel> implements StockInDetailContract.View, RefreshView.OnFreshListener,
        StockInDetailAdapter.OnItemClickLitener {
    private static final String STOCKID = "stockId:";
    @BindView(R.id.appbatlayout)
    AppBarLayout appbatlayout;
    @BindView(R.id.rlv_mine_invite)
    RecyclerView rlv_mine_invite;
    @BindView(R.id.super_refresh)
    RefreshView refreshView;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;


    private TextView tvStockCode;
    private TextView tvCreatName;
    private TextView tvDate;

    TipsHelper mTipsHelper;
    private ArrayList<StockInDetailBean.BizStockInDetailListBean> mListBeen = new ArrayList<>();
    private StockInDetailAdapter mStockInDetailAdapter;

    public static void start(Context context, String stockId) {
        Intent intent = new Intent(context, StockInDetailActivity.class);
        intent.putExtra(STOCKID, stockId);
        context.startActivity(intent);
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_stock_in_detail;
    }

    @Override
    protected void initView() {
        getTvTitle().setText(R.string.stock_in_detail);
        mTipsHelper = createTipsHelper(refreshView);
        //refreshView下拉刷新 上拉加载
        refreshView.setListener(this);
        refreshView.setHeader(new DefaultHeader(this, true));
        //mInviteAdapter recycleview适配器
        mStockInDetailAdapter = new StockInDetailAdapter(this, mListBeen);
        //设置为纵向
        rlv_mine_invite.setLayoutManager(new LinearLayoutManager(this));
        //必写
        rlv_mine_invite.setHasFixedSize(true);
        //添加分割线
        rlv_mine_invite.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
        //setAdapter  rlv_mine_invite 这儿用new HeaderAndFooterRecyclerViewAdapter(mInviteAdapter) 是 添加了头布局 直接 写setAdapter(mInviteAdapter)
        rlv_mine_invite.setAdapter(new HeaderAndFooterRecyclerViewAdapter(mStockInDetailAdapter));

        //头布局
        View view = LayoutInflater.from(this).inflate(R.layout.stock_in_detail_header_item, null);
        tvStockCode = (TextView) view.findViewById(R.id.tv_no);
        tvCreatName = (TextView) view.findViewById(R.id.tv_creat_name);
        tvDate = (TextView) view.findViewById(R.id.tv_date);
        RecyclerViewUtils.setHeaderView(rlv_mine_invite, view);

        mStockInDetailAdapter.setOnItemClickLitener(this);

//        RecyclerViewUtils.setHeaderView(rlViewInstock, LayoutInflater.from(this).inflate(R.layout.instock_detail_header_item, null));
    }

    @Override
    public void getDate() {
        //请求数据
        mListBeen.clear();
        mListBeen.clear();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideError();
        mTipsHelper.showLoading(true);
        getStockDetail();
    }

    @Override
    public void getStockInDetail(StockInDetailBean stockInDetailBean) {
        mListBeen.addAll(stockInDetailBean.getBizStockInDetailList());
        tvStockCode.setText(stockInDetailBean.getStockInCode());
        tvDate.setText(TimeUtils.milliseconds2String(stockInDetailBean.getStockInDate(), new SimpleDateFormat("yyyy年MM月dd日")));
        tvCreatName.setText(stockInDetailBean.getCreateByName());
        mStockInDetailAdapter.notifyDataSetChanged();
        if (stockInDetailBean.getBizStockInDetailList().isEmpty()) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

    private void getStockDetail() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("stockInId", getIntent().getStringExtra(STOCKID));
        mPresenter.getStockInDetail(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", getIntent().getStringExtra(STOCKID));
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
        mTipsHelper.hideLoading();
        mTipsHelper.hideEmptyType();
        mTipsHelper.hideEmpty();
        mTipsHelper.hideError();
        refreshView.onFinishFreshAndLoad();
    }

    @Override
    public void showMessage(String message) {
        //错误信息
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> getDate());
        } else {
            ToastUtils.showShort(message);
        }
    }

    //刷新
    @Override
    public void onRefresh() {
        getDate();
    }

    @Override
    public void onLoadmore() {

    }

    @Override
    public void onItemClick(View view, int position) {
        GoodsDetailActivity.start(this, mListBeen.get(position).getGoodsId());
    }
}
